/**
 * Created by Ysssssss on 2021/5/24.
 */
import apiType, {
    GET_CAN_IMPORT_TO_SHELF_PRODUCT_SELECTION,
    GET_SHELF_GROUP_LIST, GET_SHELF_GROUP_SELECTION, GET_SHELF_PRODUCT_INFO,
    GET_SHELF_PRODUCT_LIST, GET_SHELF_PRODUCT_SELECTION,
    CHECK_MENU_INFO
} from "@constants/ActionTypes";

const initialState = {
    shelfGroupList: [],
    productCanImportListPagination: {},
    shelfProductListPagination: {},
    shelfProductSelectionPagination: {},
    shelfSkuProductSelectionPagination: {},
    shelfProductInfo: {},
    shelfGroupSelection: [],
    shelfMenuInfo: {}
}

const flatShelfProductWithSku = prods => {
    let retProds = []
    prods.forEach(prod => {
        const { skus, ...prodInfo } = prod
        if (skus.length === 1 && !skus[0].name) {
            retProds.push({
                ...prodInfo,
                storage_key: `${prodInfo.id}-${skus[0].id}`,
            })
        } else {
            skus.forEach(sku => {
                retProds.push({
                    ...prodInfo,
                    storage_key: `${prodInfo.id}-${sku.id}`,
                    custom_name: prodInfo.custom_name ? `${prodInfo.custom_name}-${sku.name}` : '',
                    name: `${prodInfo.name}-${sku.name}`,
                    price_range: [sku.price, sku.price],
                })
            })
        }
    })
    return retProds
}

export default function update(state = initialState, action) {
    const {response, params, payload} = action

    switch (action.type) {
        case apiType(GET_SHELF_GROUP_LIST).success:
            return {
                ...state,
                shelfGroupList: response.groups
            }
        case apiType(GET_CAN_IMPORT_TO_SHELF_PRODUCT_SELECTION).success:
            return {
                ...state,
                productCanImportListPagination: response
            }
        case apiType(GET_SHELF_PRODUCT_LIST).success:
            return {
                ...state,
                shelfProductListPagination: response
            }
        case apiType(GET_SHELF_PRODUCT_SELECTION).success:
            return {
                ...state,
                shelfProductSelectionPagination: params.pageNo === 1 ? {
                    ...response,
                    pageNo: params.pageNo,
                } : {
                    ...state.shelfProductSelectionPagination,
                    prods: [
                        ...state.shelfProductSelectionPagination.prods,
                        ...response.prods
                    ],
                    pageNo: params.pageNo,
                },
                shelfSkuProductSelectionPagination: params.pageNo === 1 ? {
                    ...response,
                    prods: flatShelfProductWithSku(response.prods),
                    pageNo: params.pageNo,
                } : {
                    ...state.shelfSkuProductSelectionPagination,
                    prods: [
                        ...state.shelfSkuProductSelectionPagination.prods,
                        ...flatShelfProductWithSku(response.prods),
                    ],
                    pageNo: params.pageNo,
                },
            }
        case apiType(GET_SHELF_PRODUCT_INFO).success:
            return {
                ...state,
                shelfProductInfo: {
                    ...state.shelfProductInfo,
                    [params.id]: response
                }
            }
        case apiType(GET_SHELF_GROUP_SELECTION).success:
            return {
                ...state,
                shelfGroupSelection: response.prod_types
            }
        case apiType(CHECK_MENU_INFO).success:
            return {
                ...state,
                shelfMenuInfo: response
            }
        default:
            return state
    }
}